﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Logic
{
    public class ChallengePAK
    {
        ushort CRC16_163(byte[] data, int length)
        {
            // this function computes the 16-bit CRC over an array of bytes pointed to by data

            ushort crc;
            byte index;
            byte bit;
            byte parity;

            crc = 0; // start with all bits low

            // for each byte of data...
            for (index = 0; index < length; index++)
            {
                crc ^= data[index]; // XOR CRC with byte at index

                // for each bit of CRC
                for (bit = 0; bit < 8; bit++)
                {
                    parity = (byte)crc; // remember its value for the parity check
                    crc >>= 1; // right-shift the CRC
                    if (parity % 2 != 0)
                        crc ^= 0xa001; // if CRC was odd, XOR it with the CCITT polynomial (0xA001)
                }
            }

            return (crc); // and return the computed 16-bit CRC
        }

    }
}
